package com.bwsoft.portal.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.api.util.Constant;
import com.bwsoft.util.Base64Util;
import com.bwsoft.util.DesUtil;
import com.bwsoft.util.ValidatorUtil;

public class RegisterAction extends PortalBaseAction {

	private static Logger LOG = Logger.getLogger(RegisterAction.class);
	
	private int accountType;
	private String compName;
	private String linkMan;
	private String phone;
	private String address;
	private String email;
	private String password;
	private String password2;
	private String fax;
	private String cnasFlag;
	private Integer compNature;
	private File img1;
	private String img1FileName; // 文件名称
	private File img2;
	private String img2FileName; // 文件名称
	
	private String r;
	private String mobile;
	private String checkcode;
	private String loginpwd;
	private String loginpwd2;
	
	List<HashMap> natureStr;

	public String step1() {
		return "step1";
	}
	
	public String step2() {
		ResponsePojo responsePoj= dictionaryService.getData("comp_nature");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		natureStr = (List<HashMap>)returnMap.get(0).get("dicData");
		return "step2";
	}
	
	public String step3() {
		return "step3";
	}
	
	public String step4() {
		return "step4";
	}
	
	@SuppressWarnings("unchecked")
	public String sendsms() {
		try {
			Boolean canFlag=true;
	       
			if(mobile==null||mobile.trim().equals("")){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "手机号不能为空！");
				canFlag=false;
			}
			
			if(canFlag&&!ValidatorUtil.isMobile(mobile)){
				responseJson.put("errcode", 1);
				responseJson.put("errmsg", "请输入正确的手机号！");
				canFlag=false;
			}

			if (canFlag) {
				// 调用service方法得到要返回的数据对象
				LOG.info("开始调用取数方法");
				ResponsePojo rp = codeService.phoneRegister(mobile);
				LOG.info("调用取数方法结束");
				responseJson.put("errcode", rp.getErrcode());
				responseJson.put("errmsg", rp.getErrmsg());
				responseJson.put("data", rp.getData());
			}
		} catch (Exception e) {
			LOG.info("发送短信验证码出错");
			responseJson.put("errcode", 5000);
			responseJson.put("errmsg", "服务器异常");
			responseJson.put("data", e.toString());
		}
		return "mapJsonResult";
	}
		
	/**
	 * 解析参数转换成Map
	 * 
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	Map<String, Object> reflexParameters() throws IOException, Exception {

		if (r == null || r.isEmpty())
			return new HashMap<String, Object>();

		// 先用Base64解密然后用DES解密
		String parameters = DesUtil.decrypt(Base64Util.getFromBase64(r),
				Constant.KEY_DES);

		// 得到的解密字符串用&分隔参数
		String[] str = parameters.split("&");

		Map<String, Object> parameMap = new HashMap<String, Object>();

		StringBuffer sb = new StringBuffer("传入参数:");
		try {
			for (String string : str) {
				String key = string.split("=")[0];
				String value = string.split("=").length == 2 ? string
						.split("=")[1] : "";
				parameMap.put(key, value);
				sb.append("【" + key + "=" + value + "】;");
			}
		} catch (Exception e) {
			LOG.info("参数【" + parameters + "】异常：" + e);
			throw e;
		}
		LOG.info(sb.toString());
		return parameMap;
	}
	
	@SuppressWarnings("unchecked")
	public String company() {
		if (compName == null || "".equals(compName)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "机构名称不能为空！");
			return "mapJsonResult";
		} 
		if (linkMan == null || "".equals(linkMan)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "联系人不能为空！");
			return "mapJsonResult";
		}
		if (phone == null || "".equals(phone)) { 
			responseJson.put("result", 3);
			responseJson.put("msg", "联系电话不能为空！");
			return "mapJsonResult";
		}
		if (!ValidatorUtil.isMobile(phone)) { 
			responseJson.put("result", 4);
			responseJson.put("msg", "手机号格式不正确！");
			return "mapJsonResult";
		}
		if (email == null || "".equals(email)) { 
			responseJson.put("result", 5);
			responseJson.put("msg", "电子邮箱不能为空！");
			return "mapJsonResult";
		}
		if (!ValidatorUtil.isEmail(email)) { 
			responseJson.put("result", 6);
			responseJson.put("msg", "邮箱格式不正确！");
			return "mapJsonResult";
		}
		if (password == null || "".equals(password)) { 
			responseJson.put("result", 7);
			responseJson.put("msg", "密码不能为空！");
			return "mapJsonResult";
		}
		if (!password.equals(password2)) { 
			responseJson.put("result", 8);
			responseJson.put("msg", "两次输入的密码不一致！");
			return "mapJsonResult";
		}
		String img1Type = "";
		if (img1FileName != null && !"".equals(img1FileName)) {
			img1Type = img1FileName.substring(img1FileName.lastIndexOf(".")).toLowerCase();
		}
		if(img1FileName != null && !"".equals(img1FileName) && !qualityControlService.isImage(img1Type)){
			responseJson.put("result", 9);
			responseJson.put("msg", "证件1上传的文件不是图片类型！");
			return "mapJsonResult";
		}
		if(img1FileName != null && !"".equals(img1FileName) && !newsService.isFitSize(img1)){
			responseJson.put("result", 10);
			responseJson.put("msg", "证件1上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		String img2Type = "";
		if (img2FileName != null && !"".equals(img2FileName)) {
			img2Type = img2FileName.substring(img2FileName.lastIndexOf(".")).toLowerCase();
		}
		if(img2FileName != null && !"".equals(img2FileName) && !qualityControlService.isImage(img2Type)){
			responseJson.put("result", 11);
			responseJson.put("msg", "证件2上传的文件不是图片类型！");
			return "mapJsonResult";
		}
		if(img2FileName != null && !"".equals(img2FileName) && !newsService.isFitSize(img2)){
			responseJson.put("result", 12);
			responseJson.put("msg", "证件2上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		if (regService.checkAccount(email)) {
			responseJson.put("result", 13);
			responseJson.put("msg", "此邮箱已经注册！");
			return "mapJsonResult";
		}
		if (compName.length() > 50) { 
			responseJson.put("result", 14);
			responseJson.put("msg", "机构名称长度不能超过50！");
			return "mapJsonResult";
		}
		if (address == null || "".equals(address)) { 
			responseJson.put("result", 15);
			responseJson.put("msg", "机构地址不能为空！");
			return "mapJsonResult";
		}
		
		regAccount = new Account();
		ResponsePojo rp=regService.regCompany(regAccount,accountType,compName,linkMan,phone,address,email,password,fax,compNature,cnasFlag,img1,img1FileName,img2,img2FileName);
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public String person() {
		if (mobile == null || "".equals(mobile)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "手机号不能为空！");
			return "mapJsonResult";
		}
		if (!ValidatorUtil.isMobile(mobile)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "手机号格式不正确！");
			return "mapJsonResult";
		}
		if (loginpwd == null || "".equals(loginpwd)) { 
			responseJson.put("result", 3);
			responseJson.put("msg", "密码不能为空！");
			return "mapJsonResult";
		}
		if (!loginpwd.equals(loginpwd2)) { 
			responseJson.put("result", 4);
			responseJson.put("msg", "两次输入的密码不一致！");
			return "mapJsonResult";
		}
		
		if (!regService.checkCode(mobile, checkcode))
		{
			responseJson.put("result", 5);
			responseJson.put("msg", "验证码不正确！");
			return "mapJsonResult";
		}
		if (regService.checkAccount(mobile)) {
			responseJson.put("result", 6);
			responseJson.put("msg", "此手机号已经注册！");
			return "mapJsonResult";
		}
		
		regAccount = new Account();
		ResponsePojo rp=regService.regPerson(regAccount,accountType,mobile,loginpwd);
		responseJson.put("result", rp.getErrcode());
		responseJson.put("msg", rp.getErrmsg());
		return "mapJsonResult";
	}

	public int getAccountType() {
		return accountType;
	}

	public void setAccountType(int accountType) {
		this.accountType = accountType;
	}

	public String getCompName() {
		return compName;
	}

	public void setCompName(String compName) {
		this.compName = compName;
	}

	public String getLinkMan() {
		return linkMan;
	}

	public void setLinkMan(String linkMan) {
		this.linkMan = linkMan;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getCnasFlag() {
		return cnasFlag;
	}

	public void setCnasFlag(String cnasFlag) {
		this.cnasFlag = cnasFlag;
	}

	public Integer getCompNature() {
		return compNature;
	}

	public void setCompNature(Integer compNature) {
		this.compNature = compNature;
	}

	public File getImg1() {
		return img1;
	}

	public void setImg1(File img1) {
		this.img1 = img1;
	}

	public String getImg1FileName() {
		return img1FileName;
	}

	public void setImg1FileName(String img1FileName) {
		this.img1FileName = img1FileName;
	}

	public File getImg2() {
		return img2;
	}

	public void setImg2(File img2) {
		this.img2 = img2;
	}

	public String getImg2FileName() {
		return img2FileName;
	}

	public void setImg2FileName(String img2FileName) {
		this.img2FileName = img2FileName;
	}

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

	public String getCheckcode() {
		return checkcode;
	}

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	public String getLoginpwd() {
		return loginpwd;
	}

	public void setLoginpwd(String loginpwd) {
		this.loginpwd = loginpwd;
	}

	public String getLoginpwd2() {
		return loginpwd2;
	}

	public void setLoginpwd2(String loginpwd2) {
		this.loginpwd2 = loginpwd2;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public List<HashMap> getNatureStr() {
		return natureStr;
	}

	public void setNatureStr(List<HashMap> natureStr) {
		this.natureStr = natureStr;
	}

}
